package start.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.retry.ImmediateRequeueMessageRecoverer;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: DirectRabbitConfig
 * @Description: Rabbit配置文件
 * @author LIZEMIN
 * @date 2023/3/9 10:46
 * @Version 1.0.0
 */
//@Configuration
public class RabbitConfig {

    //队列 起名：TestDirectQueue
    @Bean
    public Queue TestDirectQueue() {

        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //return new Queue("TestDirectQueue",true,true,true);

        //一般设置一下队列的持久化就好,其余两个就是默认false

        return new Queue("TestDirectQueue",true);

        //eas demo
        //return new Queue("queue.callback.FSSC-boe", true);

    }


    @Bean
    public Queue TestTopicQueue() {

        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //return new Queue("TestDirectQueue",true,true,true);

        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue("TestTopicQueue",true);

        // 正常队列与死信交换机的绑定关系
        //Map<String, Object> deadLetterParams = new HashMap<>(2);
        //deadLetterParams.put("x-dead-letter-exchange", "DeadTestTopicExchange");
        //deadLetterParams.put("x-dead-letter-routing-key","dead-dddTopic");

        //return new Queue("TestTopicQueue",false, false, false, deadLetterParams);

        //exchange.fmc.biz
        //return new Queue("queue.callback.FSSC-demo",true);
    }


    @Bean
    public Queue TestDeadTopicQueue() {
        return new Queue("DeadTestTopicQueue", true);
    }


    //Direct交换机 起名：TestDirectExchange
    @Bean
    DirectExchange testDirectExchange() {
        //return new DirectExchange("TestDirectExchange",true,true);
        //路由模式(Direct)
        return new DirectExchange("TestDirectExchange",true,false);

    }




    @Bean
    TopicExchange testTopicExchange() {

        //主题模式(Topic)
        return new TopicExchange("TestTopicExchange",true,false);

        //return new TopicExchange("exchange.fmc.biz",true,false);
    }

    @Bean
    TopicExchange deadTestTopicExchange() {
        return new TopicExchange("DeadTestTopicExchange", true, false);
    }



    //绑定  将队列和交换机绑定, 并设置用于匹配键：TestDirectRouting
    @Bean
    Binding bindingDirect() {

        //路由模式绑定
        //return BindingBuilder.bind(this.TestDirectQueue()).to(this.testDirectExchange()).with("TestDirectRouting");
        //主题模式绑定
        return BindingBuilder.bind(this.TestTopicQueue()).to(this.testTopicExchange()).with("dddTopic.#");

        //	key.queue.callback.FSSC-boe
        //return BindingBuilder.bind(this.TestTopicQueue()).to(this.testTopicExchange()).with("key.queue.callback.FSSC-demo");

    }


    @Bean
    Binding bindingDeadDirect() {
        return BindingBuilder.bind(this.TestDeadTopicQueue()).to(this.deadTestTopicExchange()).with("dead-dddTopic");

    }

/*    @Bean
    public MessageRecoverer messageRecoverer() {
        return new ImmediateRequeueMessageRecoverer();
        //return new MessageRecoverer();
    }*/

}